﻿@using iTextSharp.text;
@using iTextSharp.text.pdf;
@using iTextSharp.text.html;
@using iTextSharp.text.xml;
@using System.Collections.Generic;
@using System.Collections;

@{
    Layout = "~/_SiteLayout.cshtml";
    int rType = Convert.ToInt32(UrlData[0].ToLower());
    
     Dictionary<string,string> c = new Dictionary<string,string>();
   var custName = Request["custName"];
        var rBy = Request["rBy"];
        string custId = string.Empty;
        string rById = string.Empty;
        if(custName!=""){
           
            custId = UtilityManager.GetVal(Request["custId"]);
            if(custId==""){
                custId="0";
            }
        }
        
        if(rBy!=""){
            rById = UtilityManager.GetVal(Request["rById"]);
            if(rById==""){
                rById="0";
            }
        }
      
        if(custId!="0"){
            custName="";
        }
        c["FName"]=custName;
        c["RefByName"]=rBy;
        c["CustomerID"]=custId;
        //c["CustomerID"]=UtilityManager.GetVal(Request["custId"]);
        c["CustomerStatus"]=UtilityManager.GetVal(Request["cStatus"]);
        c["CustomerClassification"]=UtilityManager.GetVal(Request["custClass"]);
        c["Region"]=UtilityManager.GetVal(Request["region"]);
        c["Solicitor"]=UtilityManager.GetVal(Request["solicitor"]);
        c["Campaign"]=UtilityManager.GetVal(Request["campaign"]);
        c["Country"]=UtilityManager.GetVal(Request["country"]);
        c["State"]=UtilityManager.GetVal(Request["state"]);
        c["RefBy"]=UtilityManager.GetVal(Request["rBy"]);
        c["CashCardNo"]=UtilityManager.GetVal(Request["ccno"]);
        c["CCStatus"]=UtilityManager.GetVal(Request["ccstatus"]);
        c["CCDelStatus"]=UtilityManager.GetVal(Request["delStatus"]);
        c["DocStatus"]=UtilityManager.GetVal(Request["docStatus"]);
        c["PartnerBank"]=UtilityManager.GetVal(Request["pb"]);
        c["DelCourier"]=UtilityManager.GetVal(Request["courier"]);
        c["DelCourier"]=UtilityManager.GetVal(Request["courier"]);
        c["CCSDateFrom"]=UtilityManager.GetVal(Request["from_ccsdate"]);
        c["CCSDateTo"]=UtilityManager.GetVal(Request["to_ccsdate"]);
        c["DelSDateFrom"]=UtilityManager.GetVal(Request["from_delsdate"]);
        c["DelSDateTo"]=UtilityManager.GetVal(Request["to_delsdate"]);
        c["DelDateFrom"]=UtilityManager.GetVal(Request["from_deldate"]);
        c["DelDateTo"]=UtilityManager.GetVal(Request["to_deldate"]);
        c["ExpDateFrom"]=UtilityManager.GetVal(Request["from_expdate"]);
        c["ExpDateTo"]=UtilityManager.GetVal(Request["to_expdate"]);
        c["AppDateFrom"]=UtilityManager.GetVal(Request["from_appdate"]);
        c["AppDateTo"]=UtilityManager.GetVal(Request["to_appdate"]);
        c["WithEmail"]=UtilityManager.GetVal(Request["email"]);
        c["WithLandLineNo"]=UtilityManager.GetVal(Request["landlineno"]);
        c["WithMobileNo"]=UtilityManager.GetVal(Request["mobileno"]);
        c["sort"]=UtilityManager.GetVal(Request["sort"]);
        c["sortdir"]=UtilityManager.GetVal(Request["sortdir"]);
        c["SrcRegion"]=UtilityManager.GetVal(Request["src_region"]);
   
    int reportType = Convert.ToInt32(Request["report"]);
    var reportData =  ReportManager.GetData(c,reportType);
   

    DateTime today = DateTime.Today;
    //string ctr = UtilityManager.GetCounterForFileExt();
    string fileName=string.Empty;

    //var reportData =  ReportManager.GetData(c,rType);  
    var doc = new Document(PageSize.LEGAL,10,10,10,10);
    var pdf = Server.MapPath("~/Content/Templates/ReportTemplate.pdf");
    PdfPTable table = null;
    string[]  cols=null;
   switch(rType){
        case 1:
            fileName = string.Format("CashCardMasterListReport_{0}.pdf",today.ToString("MM-dd-yyy"));
            cols = new []{"CashCardAppNo","FName","LName","PermAdd","PermAddState","PermAddCity","PermAddCountry","LandLineNo","MobileNo","Gender","PlaceOfBirth","IDType","IDNumber","Employment","Employer","Nationality","SrcOfFunds"};
            table = ReportManager.GeneratePDF(1,reportData);
            doc.SetPageSize(iTextSharp.text.PageSize.LEGAL.Rotate());
            break;
        case 2:
            fileName = string.Format("ValidationListReport{0}.pdf",today.ToString("MM-dd-yyy"));
            cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","StatusDate"};
            table = ReportManager.GeneratePDF(2,reportData);
            doc.SetPageSize(iTextSharp.text.PageSize.LEGAL.Rotate());
            break;
        case 3:
            fileName = string.Format("PendingApplicationListReport_{0}.pdf",today.ToString("MM-dd-yyy"));
            cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","StatusDate"};
            table = ReportManager.GeneratePDF(3,reportData);
            doc.SetPageSize(iTextSharp.text.PageSize.LEGAL.Rotate());
            break;
        case 4:
            fileName = string.Format("ApplicationInProgressReport{0}.pdf",today.ToString("MM-dd-yyy"));
            cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","StatusDate"};
            table = ReportManager.GeneratePDF(4,reportData);          
            doc.SetPageSize(iTextSharp.text.PageSize.LEGAL.Rotate());
            break;
       
            
    }
    var grid = new WebGrid(reportData,canSort:false,columnNames:cols,ajaxUpdateContainerId: "grid",ajaxUpdateCallback: "addCheck");
    string gridHtml = grid.GetHtml().ToString();
    
    PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));
   /* StringReader stringReader = new StringReader(gridHtml); 
    iTextSharp.text.html.simpleparser.StyleSheet  styles = new iTextSharp.text.html.simpleparser.StyleSheet();
    styles.LoadTagStyle("body", "face", "Arial");
    styles.LoadTagStyle("body", "size", "8px");
    //styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.BORDER, "1px");
    //styles.LoadTagStyle("body", "border", "1px solid black");

    List<iTextSharp.text.IElement> parsedList = (List<iTextSharp.text.IElement>) iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(stringReader, styles);
    doc.Open();
    foreach (object item in parsedList) {
        doc.Add((IElement)item);
    }*/
  
    doc.Open();
    doc.Add(table);
    doc.Close();
    
    Response.AddHeader("Content-Type", "application/pdf");
    Response.AddHeader("content-disposition", "attachment;filename="+fileName);
    Response.TransmitFile(pdf);
}
